package com.ruoyi.project.tickets.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.tickets.domain.Stock;
import com.ruoyi.project.tickets.domain.TicketsRule;
import com.ruoyi.project.tickets.domain.dto.PushDto;
import com.ruoyi.project.tickets.domain.vo.PushVo;
import com.ruoyi.project.tickets.service.IStockService;
import com.ruoyi.project.tickets.service.ITicketsRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.tickets.mapper.PushMapper;
import com.ruoyi.project.tickets.domain.Push;
import com.ruoyi.project.tickets.service.IPushService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发布票券Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-29
 */
@Service
public class PushServiceImpl implements IPushService 
{
    @Autowired
    private PushMapper pushMapper;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private ITicketsRuleService ticketsRuleService;

    @Autowired
    private IStockService stockService;
    /**
     * 查询发布票券
     * 
     * @param id 发布票券ID
     * @return 发布票券
     */
    @Override
    public Push selectPushById(Long id)
    {
        return pushMapper.selectPushById(id);
    }

    /**
     * 查询发布票券列表
     * 
     * @param push 发布票券
     * @return 发布票券
     */
    @Override
    public List<PushVo> selectPushList(Push push)
    {
        return pushMapper.selectPushList(push);
    }

    /**
     * 新增发布票券
     * 
     * @param pushList 发布票券
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPush(List<PushDto> pushList)
    {
        SysUser sysUser=tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        //先初始化发布
        for(PushDto dto:pushList){
            Push push=new Push();
            push.setRuleId(dto.getRuleId());
            push.setPubNum(dto.getNumber());
            push.setPubUserId(sysUser.getUserId());
            push.setPubUserName(sysUser.getNickName());
            TicketsRule ticketsRule= ticketsRuleService.selectTicketsRuleById(dto.getRuleId());
            push.setSalePrice(ticketsRule.getTotalPrice());
            push.setCreateTime(new Date());
            pushMapper.insertPush(push);

        }
        for(PushDto dto:pushList){
            Stock stock=new Stock();
            stock.setRuleId(dto.getRuleId());
            //stock.setShopId(dto.getShopId());
            Stock    check=stockService.selectStockByShopAndRule(stock);
            if(ObjectUtil.isNull(check)){
                stock.setStockNum(dto.getNumber());
                stockService.insertStock(stock);
            }else{
                stock.setStockNum(check.getStockNum()+dto.getNumber());
                stock.setId(check.getId());
                stockService.updateStock(stock);
            }
        }

        return 1;
    }

    /**
     * 修改发布票券
     * 
     * @param push 发布票券
     * @return 结果
     */
    @Override
    public int updatePush(Push push)
    {
        push.setUpdateTime(DateUtils.getNowDate());
        return pushMapper.updatePush(push);
    }

    /**
     * 批量删除发布票券
     * 
     * @param ids 需要删除的发布票券ID
     * @return 结果
     */
    @Override
    public int deletePushByIds(Long[] ids)
    {
        return pushMapper.deletePushByIds(ids);
    }

    /**
     * 删除发布票券信息
     * 
     * @param id 发布票券ID
     * @return 结果
     */
    @Override
    public int deletePushById(Long id)
    {
        return pushMapper.deletePushById(id);
    }
}
